let query = window.location.search.substring(1);
let blog = query.split("=");
window.localStorage.setItem("blogId", blog[1]);
let blog_id = window.localStorage.getItem("blogId");
let user_name=window.localStorage.getItem("loginName");
let n;
if(blog_id=="0") {
     n = 0;
}
else
    n=1;
window.onload=function getdata(){
    getArticle(blog_id);
    new Vue({
        el:"#first",
        data(){
            return{
                firsts:[]
            }
        },
        mounted(){
            let _this=this;
            ajax({
                method: "post",
                url: "../../update",
                data: {
                    userName:user_name,
                    blogId:blog_id,
                    code:0
                },
                success: function (result) {
                    console.log(result);
                    _this.firsts=result;
                },
                error:function (result){
                    console.log(result);
                }
            })
        }
    })
}

function getArticle(e){
    new Vue({
        el: "#next",
        data() {
            return {
                nexts: []
            }

        },
        mounted() {
            let _this = this;
            ajax({
                method: "post",
                url: "../../browse",
                data: {
                    blogId: e,
                    code: 0
                },
                success: function (result) {
                    console.log(result);
                    _this.nexts = result;
                },
                error: function () {
                }
            })
        }
    })
}



function update(){
    blog_id=window.localStorage.getItem("blogId");
    alert("确认提交");
    ajax({
        type:"post",
        url:"../../update",
        data:{
            searchPath:"http://localhost:8080/Wistudio_war_exploded2/static/html/update.html?blogId="+blog_id,
            blogId:blog_id,
            code:2
        },
        success:function (){
            alert("上传成功");
        },
        error:function () {
            alert("上传失败");
        }
    })
}
function fdraft_click() {
    let title=  document.getElementById("title");//获取输入框元素
    let description=  document.getElementById("description");
    let obj=document.getElementById("domain");
    let domain=obj.options[obj.selectedIndex].value;
    let blog_id = window.localStorage.getItem("blogId");
    console.log(blog_id);
    console.log(n);
    if(blog_id=="0"){
        ajax({
            type:"post",
            url:"../../update",
            data:{
                userName:user_name,
                blogId:blog_id,
                title:title.value,
                description:description.value,
                domain:domain,
                code:3
            },
            success:function (result){
                window.localStorage.setItem("blogId",result['result']);
                blog_id=result['result'];
            },
            error:function () {
                alert("上传失败")
            }
        })
        if(blog_id=="0"){
            alert("请重新点击保存")
            return n=1;
        }
    }
    if(n==1) {
        ajax({
            type: "post",
            url: "../../update",
            async: false,
            data: {
                userName: user_name,
                blogId: blog_id,
                title: title.value,
                description: description.value,
                domain: domain,
                code: 3
            },
            success: function (result) {
                window.localStorage.setItem("blogId", result['result'])
                alert("上传成功")
            },
            error: function () {
                alert("上传失败")
            }
        })
    }

}
function ndraft_click() {
    let article = document.getElementById("article");
    let context=encodeURI(article.value);
    let blog_id = window.localStorage.getItem("blogId");
    console.log(blog_id);
    console.log(n);
    if (blog_id == "0") {
        ajax({
            type: "post",
            url: "../../update",
            async: false,
            data: {
                userName: user_name,
                blogId: blog_id,
                article: article.value,
                code: 4
            },
            success: function (result) {
                window.localStorage.setItem("blogId", result['result'])
                blog_id = result['result'];
                alert("上传成功")
            },
            error: function () {
                alert("上传失败")
            }

        })
        if (blog_id == "0") {
            alert("请重新点击保存")
            return n = 1;
        }
    }
    if (n == 1) {
        ajax({
            type: "post",
            url: "../../update",
            async: false,
            data: {
                userName: user_name,
                blogId: blog_id,
                article: context,
                code: 4
            },
            success: function (result) {
                window.localStorage.setItem("blogId", result['result'])
                alert("上传成功")
            },
            error: function () {
                alert("上传失败")
            }
        })
    }
}

let base64img;

function pic_click(){
    let blog_id = window.localStorage.getItem("blogId");
    ajax({
        type:"post",
        url:"../../picture",
        data:{
            blogId:blog_id,
            picture:base64img,
            code:0
        },
        success:function (result){
            alert("上传成功");
            console.log(result);
        },
        error:function (result){
            console.log(result);
        }
    })
}

function img_base (){
    //获取选中图片对象（包含文件的名称、大小、类型等，如file.size）
    let file = document.getElementById("imgFile").files[0];
    // 声明js的文件流
    let reader = new FileReader();
    if(file){
        //通过文件流将文件转换成Base64字符串
        reader.readAsDataURL(file);
        reader.onloadend = function(){
            // 把转换过得base赋值给全局变量
            // 这里reader.result就是转换好的bsae64格式图片
            base64img = reader.result;
        }
    }
}
